package cn.tedu.ems.controller;

import cn.tedu.ems.pojo.dto.CourseAddNewDTO;
import cn.tedu.ems.pojo.dto.CourseUpdateDTO;
import cn.tedu.ems.pojo.vo.CourseListItemVO;
import cn.tedu.ems.pojo.vo.CourseStandardVO;
import cn.tedu.ems.service.ICourseService;
import cn.tedu.ems.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 处理课程相关请求的控制器
 */
@Validated
@Slf4j
@Api(tags = "02. 课程管理模块")
@RestController
@RequestMapping("/course")
public class CourseController {
    @Autowired
    ICourseService courseService;

    @ApiOperation("添加课程")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(@Valid CourseAddNewDTO courseAddNewDTO){
        log.debug("开始处理【添加课程】的请求，参数：{}",courseAddNewDTO);
        courseService.addNew(courseAddNewDTO);
        return JsonResult.ok();
    }

    @ApiOperation("根据id删除课程")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "课程id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> delete(@Range(min = 1, message = "删除课程失败，ID无效！")
                                   @PathVariable Long id) {
        log.debug("开始处理【根据id删除课程】的请求，参数：{}", id);
        courseService.delete(id);
        return JsonResult.ok();
    }


    @ApiOperation("修改课程详情")
    @ApiOperationSupport(order = 300)
    @PostMapping("/update")
    public JsonResult<Void> updateById(Long id, CourseUpdateDTO courseUpdateDTO) {
        log.debug("开始处理【修改课程详情】的请求，参数ID：{}, 新数据：{}", id, courseUpdateDTO);
        courseService.updateById(id, courseUpdateDTO);
        return JsonResult.ok();
    }

    @ApiOperation("查询课程列表")
    @ApiOperationSupport(order = 400)
    @GetMapping("")
    public JsonResult<List<CourseListItemVO>> list() {
        log.debug("开始处理【查询课程列表】的请求，无参数");
        List<CourseListItemVO> list = courseService.list();
        return JsonResult.ok(list);
    }

    /**
     * 根据id查询课程详情
     * @param id 课程id
     * @return 课程详情
     */
    @ApiOperation("根据id查询课程详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParam(name = "id",value = "课程id",required = true,dataType = "long")
    @GetMapping("selectById")
    public JsonResult<CourseStandardVO> getStandardById(@Range(min = 1,message = "查询失败，id无效！")
                                                       Long id) {
        log.debug("开始处理【根据id查询课程详情】的请求,参数:{}",id);
        CourseStandardVO result = courseService.getStandardById(id);
        return JsonResult.ok(result);
    }

    /**
     * 根据搜索词查询课程详情
     * @param wd 搜索词
     * @return 课程详情
     */
    @ApiOperation("根据搜索词查询课程详情")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParam(name = "wd",value = "搜索词",required = true,dataType = "string")
    @GetMapping("/selectByWd")
    public JsonResult<List<CourseStandardVO>> getStandardByWd(String wd) {
        log.debug("开始处理【根据搜索词查询课程详情】的请求,参数:{}",wd);
        List<CourseStandardVO> result = courseService.getStandardByWd(wd);
        return JsonResult.ok(result);
    }
}
